<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="商品名称" prop="productName">
        <el-input
          v-model="queryParams.productName"
          placeholder="请输入商品名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="商品描述" prop="productDescription">
        <el-input
          v-model="queryParams.productDescription"
          placeholder="请输入商品描述"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="商品价格" prop="productPrice">
        <el-input-number
          v-model="productPrice.beginProductPrice"
          controls-position="right"
          :step="10"
          :min="0"
          placeholder="最低"
          @keyup.enter.native="handleQuery"/>
        <span> 至 </span>
        <el-input-number
          v-model="productPrice.endProductPrice"
          controls-position="right"
          :step="10"
          :min="0"
          placeholder="最高"
          @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="原始价格" prop="productOriginalPrice">
        <el-input-number
          v-model="productPrice.beginProductOriginalPrice"
          controls-position="right"
          :step="10"
          :min="0"
          placeholder="最低"
          @keyup.enter.native="handleQuery"/>
        <span> 至 </span>
        <el-input-number
          v-model="productPrice.endProductOriginalPrice"
          controls-position="right"
          :step="10"
          :min="0"
          placeholder="最高"
          @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['commerce:products:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['commerce:products:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['commerce:products:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          :disabled="multiple"
          @click="handleExport"
          v-hasPermi="['commerce:products:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="productsList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="商品ID" align="center" prop="productId" v-if="true" width="80"/>
      <el-table-column label="商品名称" align="center" prop="productName" />
      <el-table-column label="商品描述" align="center" prop="productDescription" />
      <el-table-column label="商品价格" align="center" prop="productPrice" />
      <el-table-column label="原始价格" align="center" prop="productOriginalPrice" />
      <el-table-column label="商品图片" align="center" prop="productImagePreview" width="210">
        <template slot-scope="scope">
          <image-preview :src="scope.row.productImagePreview" :width="50" :height="50"/>
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center" key="status">
        <template slot-scope="scope">
          <el-switch
              v-model="scope.row.status"
              active-value="0"
              inactive-value="1"
              @change="handleStatusChange(scope.row)"
          ></el-switch>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click.stop="handleUpdate(scope.row)"
            v-hasPermi="['commerce:products:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click.stop="handleDelete(scope.row)"
            v-hasPermi="['commerce:products:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改商品对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="70%" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="商品名称" prop="productName">
          <el-input v-model="form.productName" placeholder="请输入商品名称" />
        </el-form-item>
        <el-form-item label="商品描述" prop="productDescription">
          <el-input v-model="form.productDescription" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="商品价格" prop="productPrice">
          <el-input v-model="form.productPrice" placeholder="请输入商品价格" />
        </el-form-item>
        <el-form-item label="商品原始价格" prop="productOriginalPrice">
          <el-input v-model="form.productOriginalPrice" placeholder="请输入商品原始价格" />
        </el-form-item>
        <el-form-item label="商品图片" prop="productImage">
          <image-upload v-model="form.productImage"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button :loading="buttonLoading" type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listProducts,
  getProducts,
  delProducts,
  addProducts,
  updateProducts,
  changeProductStatus
} from '@/api/commerce/products'
import {required} from "@/utils/rules";

export default {
  name: "Products",
  data() {
    return {
      // 按钮loading
      buttonLoading: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 商品表格数据
      productsList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        productName: undefined,
        productDescription: undefined,
        productImage: undefined,
      },
      productPrice:{
        beginProductPrice: undefined,
        endProductPrice: undefined,
        beginProductOriginalPrice: undefined,
        endProductOriginalPrice: undefined,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        productId: [required],
        productName: [required],
        productDescription: [required],
        productPrice: [required],
        productOriginalPrice: [required],
        productImage: [required],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询商品列表 */
    getList() {
      this.loading = true;
      this.queryParams.params = {};
      if (null != this.productPrice && '' !== this.productPrice) {
        this.queryParams.params["beginProductPrice"] = this.queryParams.beginProductPrice;
        this.queryParams.params["endProductPrice"] = this.queryParams.endProductPrice;
        this.queryParams.params["beginProductOriginalPrice"] = this.queryParams.beginProductOriginalPrice;
        this.queryParams.params["endProductOriginalPrice"] = this.queryParams.endProductOriginalPrice;
      }
      listProducts(this.queryParams).then(response => {
        this.productsList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        productId: undefined,
        productName: undefined,
        productDescription: undefined,
        productPrice: undefined,
        productOriginalPrice: undefined,
        productImage: undefined,
        productImagePreview: undefined,
        status: undefined,
        createBy: undefined,
        createTime: undefined,
        updateBy: undefined,
        updateTime: undefined
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.productId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加商品";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.loading = true;
      this.reset();
      const productId = row.productId || this.ids
      getProducts(productId).then(response => {
        this.loading = false;
        this.form = response.data;
        this.open = true;
        this.title = "修改商品";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.buttonLoading = true;
          if (this.form.productId != null) {
            updateProducts(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            }).finally(() => {
              this.buttonLoading = false;
            });
          } else {
            addProducts(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            }).finally(() => {
              this.buttonLoading = false;
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const productIds = row.productId || this.ids;
      this.$modal.confirm('是否确认删除商品编号为"' + productIds + '"的数据项？').then(() => {
        this.loading = true;
        return delProducts(productIds);
      }).then(() => {
        this.loading = false;
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      }).finally(() => {
        this.loading = false;
      });
    },
    /** 全部导出按钮操作 */
    handleAllExport() {
      this.download('commerce/products/exportAll', {
        ...this.queryParams
      }, `products_${new Date().getTime()}.xlsx`)
    },
    /** 选择项导出按钮操作 */
    handleExport(row) {
      const productIds = row.productId || this.ids;
      this.download('commerce/products/exportSelect/'+productIds, {
        ...this.queryParams
      }, `products_${new Date().getTime()}.xlsx`)
    },
    // 用户状态修改
    handleStatusChange(row) {
      let text = row.status === "0" ? "启用" : "停用";
      this.$modal.confirm('确认要"' + text + '""' + row.productName + '"商品吗？').then(function() {
        return changeProductStatus(row.productId, row.status);
      }).then(() => {
        this.$modal.msgSuccess(text + "成功");
      }).catch(function() {
        row.status = row.status === "0" ? "1" : "0";
      });
    },
  }
};
</script>
